<?php

class Controller_Wwwmall_Add extends Controller_Wwwmall_Abstract
{

    function showMessage($message, $msgType, $url = null)
    {
        echo "<script>alert('{$message}');";
        if ($url) echo "location='{$url}';";
        echo "</script>";
    }


    /*
     *地址添加
     */
    function defaultAction()
    {
       ;
    }
    /*
    *
    */
    public function inAction()
    {
        $this->openDebug();
        $cityList = Service_Address::getCityList();
        $county= Service_Address::getCountyList();

//         var_dump($cityList);die;
        $this->assign('cityList',$cityList);
        $this->assign('county',$county);

        $this->display('add_address');
    }
    //增加收货地址
    public function getaddressAction()
    {
        global $_F;
        $uid=$_F['uid'];
        $this->openDebug();
        $id=  FRequest::getInt('auto_id')?FRequest::getInt('auto_id'):"";
        $contact = FRequest::getString('contact')? FRequest::getString("contact") : "";
        $phone = FRequest::getString("phone")? FRequest::getString("phone") : "";
        $city = FRequest::getString("city")? FRequest::getString("city") : "";
        $county = FRequest::getString("county")? FRequest::getString("county") : "";
        $province = FRequest::getString("province")? FRequest::getString("province") : "";
        $housemember = FRequest::getString("address")? FRequest::getString("housemember") : "";
        $address = FRequest::getString("address")? FRequest::getString("address") : "";
        $sex = FRequest::getInt('sex')? FRequest::getInt("sex") : 1;
        $lat = FRequest::getString('lat')? FRequest::getString("lat") : "";
        $lng =FRequest::getString('lng')? FRequest::getString("lng") : "";
        if(empty($county)){
            $result = Service_Location::getAddressFromXY($lng,$lat);
            $county = $result['district'];
        }
        Service_Address::saveAddress($id,$uid,$contact,$phone,$city,$province,$county,$address,$housemember,$sex,$lat,$lng,$is_default=1);

//        if($id>0){
//             $this->success("修改成功","/wwwmall/add/")
//        }
        $result = array("status" => 200, 'success' => "修改成功");
        FResponse::output($result);
    }

    //获取全部收货地址
    public function addlistAction(){
        global $_F;
        $uid=$_F['uid'];
        $isback=FCookie::get('isback');
        if($isback){
            $this->assign('back_url','/order/submit');
        }
        $data=Service_Address::getAddressList($uid);
        $this->assign('data',$data);
        $this->display('adress_manage');

//      echo "<pre>"; print_r($data);echo "</pre>";die;
    }

    //修改删除后默认收货地址
    public  function  updateAddressAction(){
        global $_F;
        $uid=$_F['uid'];
        $dataid=Service_Address::resetDefaultAddress($uid);
        $this->assign('dataid',$dataid);
//        $this->display('adress_manage');
    }
    //获取单个收货地址详情
    public  function  getOneAddressAction(){
        $id=  FRequest::getInt('id');
        $province=FRequest::getString('province');
        $city=FRequest::getString('city');
        $county=FRequest::getString('county');
        $detail=FRequest::getString('detail');
        $lng=FRequest::getString('lng');
        $lat=FRequest::getString('lat');
        $shaddress=$county.$detail;

        $addressdata=Service_Address::getAddress($id);
        if(empty($city)){
            $city = $addressdata['city'];
        }
        if(empty($province)){
            $province = $addressdata['province'];
        }
        if(empty($lng) || empty($lat)){
            $lng = $addressdata['lng'];
            $lat = $addressdata['lat'];
        }
//        echo "<pre>";print_r($addressdata);die;
//        $this->assign('diqu',$diqu);
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('county',$county);
        $this->assign('shaddress',$shaddress);
        $this->assign('address',$addressdata);
        $isback = FCookie::get('isback');
        $this->assign('isback',$isback);
        $this->assign('id',$id);
        $this->assign('lng',$lng);
        $this->assign('lat',$lat);
        $this->display('adress_update');
    }
    //从百度地图返回来的收货地址详情
    public  function  getBaiduAddressAction(){
        $id=  FRequest::getInt('id');
        $province=FRequest::getString('province');
        $city=FRequest::getString('city');
        $county=FRequest::getString('county');
        $detail=FRequest::getString('detail');
        $nick=FRequest::getString('nick');
        $mobile=FRequest::getString('mobile');
        $sex=FRequest::getString('sex');
        $housemember=FRequest::getString('housemember');
        $lng=  FRequest::getString('lng');
        $lat=  FRequest::getString('lat');
        $shaddress=$county.$detail;

        $addressdata=Service_Address::getAddress($id);
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('county',$county);
        $this->assign('shaddress',$shaddress);
        $this->assign('address',$addressdata);
        $this->assign('nick',$nick);
        $this->assign('lng',$lng);
        $this->assign('lat',$lat);
        $this->assign('mobile',$mobile);
        $this->assign('sex',$sex);
        $this->assign('housemember',$housemember);
        $isback = FCookie::get('isback');
        $this->assign('isback',$isback);
        $this->assign('id',$id);
        $this->display('add_address');
    }


     //修改单个默认地址详情
    public  function  updateDeaultAction(){
        global $_F;
        $uid=$_F['uid'];
        $id= FRequest::getInt('id');
        Service_Address::resetDefaultAddressId($id,$uid);
        $this->success('修改成功',"r");
    }

    //获得默认收货地址
        public  function  getDefaultAction(){
            global $_F;
            $uid=$_F['uid'];
        $defaultAddress=Service_Address:: getDefaultAddress($uid);
            $this->assign('defaultData',$defaultAddress);
            $this->display('adress_manage');
//       echo "<pre>"; print_r($defaultAddress);echo "</pre>";die;
    }



     //删除收货地址
    public   function  delAction(){
        global $_F;
        $uid=$_F['uid'];
        $id=  FRequest::getInt('id');
        Service_Address::delAddress($uid,$id);
        $this->success("删除成功", "/wwwmall/add/addlist"."?id=" . $id);
    }

    //根据地图找位置

     public  function  LocationAction(){
         global $_F;
         $string=$_F['info']['zb'];
         list($string_x,$string_y) = explode(',',$string);
//         echo $string_x;
//         echo "<br/>" ;
//         echo $string_y;die;

         $id=  FRequest::getInt('id')?FRequest::getInt('id'):"";
         $nick= FRequest::getString('nick')? FRequest::getString("nick") : "";
         $mobile = FRequest::getString("mobile")? FRequest::getString("mobile") : "";
         $sex = FRequest::getString("sex")? FRequest::getString("sex") : "";
         $housemember = FRequest::getString("housemember")? FRequest::getString("housemember") : "";
         $this->assign('id',$id);
         $this->assign('nick',$nick);
         $this->assign('mobile',$mobile);
         $this->assign('sex',$sex);
         $this->assign('nick',$nick);
         $this->assign('string_x',$string_x);
         $this->assign('string_y',$string_y);
         $this->assign('housemember',$housemember);
         $this->display('baiduditu');

     }



}